package com.li.sort.arithmetic;

import java.util.Arrays;
import java.util.Random;

public class ShellSort {
	public static final int SIZE = 9;
	public static void main(String[] args) {
		int[] array = new int[SIZE];
		Random random = new Random();
		for(int i = 0;i < SIZE ;i++){
			array[i] = random.nextInt(100);
		}
		
		System.out.println(Arrays.toString(array));
		shellSort(array,array.length);
		System.out.println(Arrays.toString(array));
	}
	
	/**
	 * n-1趟就ok
	 * @param array
	 */
	public static void shellSort(int[] array,int n){
		int i,j;
		int temp;
		while((n /= 2) > 0){
			for(i = n;i < array.length;i++){
				temp = array[i];
				j = i-n;
				while(j>=0 && temp < array[j]){
					array[j+n] = array[j];
					j -= n;
				}
				array[j+n] = temp;
			}
		}
	}
}
